/**
 * 这是个经常使用的，简化的建造者模式。
 * 
 * 移除了【抽象建造者】、【指挥者】，私有构造
 */
public class SimpleBean {
    private String name;
    private int age;
    private String sex;
    private SimpleBean(SimpleBeanBuilder builder){
        this.name = builder.name;
        this.age = builder.age;
        this.sex = builder.sex;
    }

    public void print(){
        System.out.println(sex);
        System.out.println(name);
        System.out.println("age:"+age);
    }


    public static class SimpleBeanBuilder{
        private String name;
        private int age;
        private String sex;

        public SimpleBeanBuilder(String name){//可以在构造中设置必传参数
            this.name = name;
        }

        public void setAge(int age){
            this.age = age;
        }

        public SimpleBean build(String sex){//这里也可以设置必传参数，等同于构造
            if (this.age >10) {//建造者中还可以在构建时设定限制
                throw new IllegalArgumentException("Age must be small than 10");
            }
            this.sex = sex;
            return new SimpleBean(this);
        }
    }
}
